<!-- 首席服务员 -->
<script setup lang="ts">
import moment from "moment";
import SearchItem from "../../components/SearchItem.vue";
import TableCell from "../../components/TableCell.vue";
import EditDrawer from "../../components/EditDrawer.vue";
import Editor from "../../components/Editor.vue";
import { ElMessage, valueEquals } from "element-plus";
import { ref, reactive } from "vue";
import useTable from "./useTable";
import useForm from "./useForm";
import { useRoute } from "vue-router";
import { useRoleStore } from "../../store/role.js";
import { deleteChiefAPI } from "../../api/chief"
import ImgCorpper from "../../components/ImgCorpper.vue";

const path = useRoute().path;
const permissions = useRoleStore().permissions;
const permission = permissions[path] || {};

const logVisible = ref(false);
const corpper = ref()
// 查询条件
let filters = reactive({
    name: "",
    pageNo: 1,
    pageSize: 10,
});

// 列表数据
const { tableData, loading, totalCount, query } =
    useTable(filters);

// 编辑表单
const {
    dialogVisible,
    formRef,
    isEdit,
    form,
    upload,
    editor,
    handleEdit,
    handleConfirm,
} = useForm(query, corpper);

// 删除
const deleteHandle = (id: string) => {
    deleteChiefAPI({
        id,
    }).then((res) => {
        if (res.code === 200) {
            ElMessage({
                message: "删除成功",
                type: "success",
            });
            query();
        }
    });
};

const rules = reactive({
    title: [
        { required: true, message: "请输入事项标题", trigger: "blur" },
        { min: 1, max: 100, message: "长度不超过100个字", trigger: "blur" },
    ],
    name: [{ required: true, message: "请输入姓名", trigger: "blur"}],
    duty: [{ required: true, message: "请输入职务", trigger: "blur"}],
    dept: [{ required: true, message: "请输入部门", trigger: "blur"}],
    phone: [{ required: true, message: "请输入联系电话", trigger: "blur"}],
    file: [
        {
            required: true,
            // 自定义规则
            validator: (rule: any, value: any, callback: any) => {
                if (corpper.value.imgUrl != '' && corpper.value.imgUrl != null && corpper.value.imgUrl !== undefined) {
                    callback();
                } else {
                    callback(new Error("请上传照片并确定"));
                }
            },
        },
    ],
});
</script>

<template>
    <div>
        <!-- 搜索框 -->
        <el-card class="search-wrapper common-card">
            <el-row :gutter="24">
                <SearchItem label="姓名">
                    <el-input clearable style="width: 100%" placeholder="请输入姓名" v-model="filters.name"></el-input>
                </SearchItem>
            </el-row>
            <div class="search-button-wrapper">
                <el-button v-if="permission['新增']" :icon="Plus" plain auto-insert-space @click="() => handleEdit(false)">新增</el-button>
                <el-button v-if="permission['查询']" type="primary" :icon="Search" auto-insert-space @click="query">查询</el-button>
            </div>
        </el-card>
        <!-- 列表 -->
        <el-card class="common-card" style="width: 100%">
            <!-- 列表 -->
            <el-table v-loading="loading" :data="tableData" stripe>
                <el-table-column prop="id" label="id" width="80" />
                <el-table-column prop="name" label="姓名" />
                <el-table-column prop="duty" label="职务" />
                <el-table-column prop="dept" label="部门" />
                <el-table-column prop="phone" label="联系电话" />
                <el-table-column prop="fileName" label="照片">
                    <template #default="scope">
                        <el-link type="primary" :href="scope.row.filePath" target="_blank">{{scope.row.fileName}}</el-link>
                    </template>
                </el-table-column>
                <el-table-column prop="createDate" label="创建时间" width="200" />
                <el-table-column prop="updateDate" label="更新时间" width="200" />
                <el-table-column fixed="right" label="操作" width="180">
                    <template #default="scope">
                        <!-- 编辑 -->
                        <el-button v-if="permission['编辑']" type="text" @click="() => handleEdit(scope.row)">编辑</el-button>
                        <!-- 删除 -->
                        <el-popconfirm v-if="permission['删除']" :title="`确定删除吗?`" @confirm="deleteHandle(scope.row.id)">
                            <template #reference>
                                <el-button class="color_danger" type="text">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </template>
                </el-table-column>
                <template #empty>
                    <el-empty description="暂无数据"></el-empty>
                </template>
            </el-table>
            <!-- 分页 -->
            <el-pagination style="margin-top: 20px; justify-content: flex-end" background layout="total, sizes, prev, pager, next" :total="totalCount" :default-current-page="1" v-model:current-page="filters.pageNo" v-model:page-size="filters.pageSize"></el-pagination>
        </el-card>
        <!-- 新增/编辑表单 -->
        <EditDrawer :width="850" :title="isEdit ? '编辑' : '新增'" :visible="dialogVisible" @update:visible="dialogVisible = $event" @confirm="handleConfirm">
            <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="姓名" prop="name">
                    <el-input style="width: 400px" maxlength="100" v-model="form.name" placeholder="请输入姓名"></el-input>
                </el-form-item>
                <el-form-item label="职务" prop="duty">
                    <el-input style="width: 400px" maxlength="100" v-model="form.duty" placeholder="请输入职务"></el-input>
                </el-form-item>
                <el-form-item label="部门" prop="dept">
                    <el-input style="width: 400px" maxlength="100" v-model="form.dept" placeholder="请输入部门"></el-input>
                </el-form-item>
                <el-form-item label="联系电话" prop="phone">
                    <el-input style="width: 400px" maxlength="100" v-model="form.phone" placeholder="请输入联系电话"></el-input>
                </el-form-item>
                <el-form-item label="照片" prop="file">
                    <div style="width: 600px; height: 270px;">
                      <ImgCorpper ref="corpper" :photoPath="form.filePath"/>
                    </div>
                </el-form-item>
            </el-form>
        </EditDrawer>

    </div>
</template>

<style lang="less" scoped>
@import url("../../assets/less/common.less");
@import url("../../assets/less/search.less");
</style>